﻿using System;
using System.Web.UI.HtmlControls;

namespace Designit.Umb.Canonical.WebUi.usercontrols.Designit.Canonical
{
    public partial class DICanonical : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var currentNode = umbraco.presentation.nodeFactory.Node.GetCurrent();
            string currentUrl = Request.Url.AbsolutePath;
            
            if (currentUrl == "/" || currentUrl == currentNode.NiceUrl) return;

            var canonicalMeta = new HtmlLink();
            canonicalMeta.Attributes.Add("rel", "canonical");
            canonicalMeta.Href = currentNode.NiceUrl;
            if (!string.IsNullOrEmpty(Request.Url.Query))
                canonicalMeta.Href += Request.Url.Query;
            Page.Header.Controls.Add(canonicalMeta);
        }
    }
}